//
//  YSProgressImageView.m
//  Yueshi
//
//  Created by Northwan on 15/9/29.
//  Copyright © 2015年 悦食中国. All rights reserved.
//  具有进度条+上传功能的 UIImageView

#import "YSProgressImageView.h"
#import "UpYun.h"

#define PhotoW ((SCREENW-YSPublicXInset*5)/4)


@interface YSProgressImageView()
@property(nonatomic,readonly)UIActivityIndicatorView *loadingIndicator;
@property (nonatomic, assign) float progress;
@end

@implementation YSProgressImageView

-(instancetype)init{
    if(self =[super init]){
        [self initWithIndicator];
    }
    return self;
}


-(void)initWithIndicator{
    if(!_loadingIndicator)
    {
        CGFloat width = (SCREENW/4-YSPublicXInset*5)/2;
        _loadingIndicator = [[UIActivityIndicatorView alloc] init];
        _loadingIndicator.size = CGSizeMake(width, width);
        _loadingIndicator.backgroundColor = [UIColor clearColor];
    }
}

-(void)startLoadingIndicator
{
    if(!_loadingIndicator.superview && _whileShowIndicator)
    {
        [self addSubview:_loadingIndicator];
    }
    [_loadingIndicator startAnimating];
}

-(void)stopLoadingIndicator
{
    if(_loadingIndicator.superview && _whileShowIndicator)
    {
        [_loadingIndicator removeFromSuperview];
    }
    [_loadingIndicator stopAnimating];
}

- (NSString *)getUPYSaveKey{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"/yyyy/MM/dd"];
    //用[NSDate date]可以获取系统当前时间
    NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
    
    //生成32位的随机字符串
    NSString *dataPoint = [NSString randomStringWithLength:32];
    return [NSString stringWithFormat:@"/user/product/comment%@/%.0f%@",currentDateStr,[[NSDate date] timeIntervalSince1970],dataPoint];
}

/**
 *  上传图片
 *  @param image
 */
-(void)uploadImg:(UIImage *)image{
    if(_whileShowIndicator){
        _loadingIndicator.x = (self.width-_loadingIndicator.width)/2;
        _loadingIndicator.y = (self.width-_loadingIndicator.height)/2;
    }
    
    self.image = image;
    NSData *data = UIImageJPEGRepresentation(image, 1.0);
    UpYun *uy = [[UpYun alloc] init];
    __weak UpYun *uuy = uy;
    [self startLoadingIndicator];

    uy.successBlocker = ^(id data){
        [self stopLoadingIndicator];
        NSDictionary *dic = data;
        NSString *url = [dic objectForKey:@"url"];
        NSLog(@"上传图片成功：%@",url);
        if ([_delegate respondsToSelector:@selector(uploadImageSuccess:tag:)]) {
            [_delegate uploadImageSuccess:url tag:self.tag];
        }
        _isUpload = YES;
    };
    uy.failBlocker = ^(NSError * error)
    {
        [self stopLoadingIndicator];
        [uuy cancelUpLoad];
        NSString *message = [error.userInfo objectForKey:NSLocalizedDescriptionKey];
        NSLog(@"上传图片失败：%@",message);
        if ([_delegate respondsToSelector:@selector(uploadImageFailure:)]) {
            [_delegate uploadImageFailure:self.tag];
        }
    };
    uy.progressBlocker = ^(CGFloat percent, long long requestDidSendBytes)
    {
        //控制进度条
        //        [HUD setProgress:percent animated:YES];
    };
    [uy uploadFile:data saveKey:[self getUPYSaveKey]];
}

@end
